<template>
    <div>
        <el-row class="title">
            <h3>账号管理</h3>
        </el-row>
        <el-row>
            <el-form :inline="true" @submit.native.prevent>
                <el-form-item>
                    <el-button type="primary" @click="showDialog"><i class="el-icon-plus el-icon--left"></i>添加账号</el-button>
                </el-form-item>
                <el-form-item label="账号">
                    <el-input class="search-input" v-model="username" placeholder="请输入账号" clearable @change="getData"></el-input>
                </el-form-item>
            </el-form>
        </el-row>
        <el-row>
            <el-table :data="adminList" border>
                <el-table-column prop="userId" label="id" align="center"></el-table-column>
                <el-table-column prop="username" label="账号" align="center"></el-table-column>
                <el-table-column label="操作" align="center" width="160px">
                    <template slot-scope="scope">
                        <el-button size="small" type="primary" @click="updateDatas(scope.$index, scope.row)">修改</el-button>
                        <el-button size="small" type="danger" @click="deleteDatas(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-row>
        <el-dialog :title="add ? '添加' : '修改' + '视频'" :visible.sync="dialogShow" width="400px" @close="closeDialog">
            <el-form label-width="120px">
                <el-form-item label="账号">
                    <el-input v-model="admin.username" placeholder="账号"></el-input>
                </el-form-item>
                <el-form-item label="密码">
                    <el-input v-model="admin.password" placeholder="密码"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="closeDialog">取 消</el-button>
                <el-button type="primary" @click="save" :loading="loading">确 定</el-button>
            </div>
        </el-dialog>
        <!-- 分页 -->
        <el-row>
            <el-col class="pagination">
                <el-pagination @current-change="currentChange" :current-page="pageId" :page-size="pageSize" layout="total, prev, pager, next" :total="count">
                </el-pagination>
            </el-col>
        </el-row>
    </div>
</template>
<script>
import {
    getAdminLists,
    addAdmin,
    deleteAdminById,
    updateAdminById
} from '@/api/api'
export default {
    data: function() {
        return {
            adminList: [],
            add: true,
            dialogShow: false,
            loading: false,
            pageId: 1,
            pageSize: 10,
            count: 0,
            username: '',
            admin: {
                userId: null,
                username: '',
                password: ''
            }
        }
    },
    created: function() {
        this.pageId = parseInt(this.$route.query.page) || 1;
        this.getData();
    },
    methods: {
        getData: function() {
            getAdminLists({
                params: {
                    pageId: this.pageId,
                    pageSize: this.pageSize,
                    username: this.username
                }
            }).then(res => {
                console.log(res)
                this.adminList = res.list;
                this.count = res.count;
            })
        },
        showDialog: function() {
            this.add = true;
            this.dialogShow = true;
        },
        closeDialog: function() {
            this.dialogShow = false;
            this.add = false;
            this.admin = {
                userId: null,
                username: '',
                password: ''
            }
        },
        save: function() {
            if (this.add) {
                addAdmin(this.admin).then(res => {
                    this.getData();
                    this.$message({
                        message: '添加成功!',
                        duration: 2000,
                        type: 'success'
                    });
                    this.closeDialog();
                })
            } else {
                updateAdminById(this.admin.userId, this.admin).then(res => {
                    this.getData();
                    this.$message({
                        message: '修改成功!',
                        duration: 2000,
                        type: 'success'
                    });
                    this.closeDialog();
                })
            }
        },
        updateDatas: function(index, row) {
            this.admin = {
                userId: row.userId,
                username: row.username,
                password: row.password
            }
            this.dialogShow = true;
        },
        deleteDatas: function(index, row) {
            this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                deleteAdminById(row.userId).then(res => {
                    this.getData();
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        currentChange: function(val) {
            this.$router.push('?page=' + val);
            this.pageId = val;
            this.getData();
        }
    }
}
</script>
<style scoped lang="scss">
.title {
    font-size: 12px;
    margin-bottom: 5px;
    h3 {
        display: inline-block;
        padding: 5px 0;
        margin: 0;
        font-weight: normal;
        color: #000;
        border-bottom: 2px solid #000;
    }
}
</style>